package com.zs.usc.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author Zero
 * @description 统一响应结果
 * @date 2021/11/12 9:08
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel("统一响应结果")
@Builder
public class RestRst<T> {

    // 状态码
    @ApiModelProperty("状态码")
    private int status = RestCode.SUCCESS.getCode();
    // 响应消息
    @ApiModelProperty("响应消息")
    private String msg;
    // 响应时间
    @ApiModelProperty("响应时间")
    private long rspTime = System.currentTimeMillis();
    // 响应数据
    @ApiModelProperty("响应数据")
    private T data;


    /**
     * 成功
     *
     * @param t 数据
     * @return 返回结果
     */
    public static <T> RestRst buildSuccess(T t) {
        return RestRst.builder()
                .msg(RestCode.SUCCESS.getMessage())
                .status(RestCode.SUCCESS.getCode())
                .data(t)
                .rspTime(System.currentTimeMillis())
                .build();
    }

    /**
     * @param status 状态码
     * @param msg    消息
     * @param t      数据
     * @param <T>
     * @return 返回结果
     */
    public static <T> RestRst build(int status, String msg, T t) {
        return RestRst.builder()
                .status(status)
                .msg(msg)
                .data(t)
                .rspTime(System.currentTimeMillis())
                .build();
    }

    /**
     * @param status 状态码
     * @param msg    消息
     * @param <T>
     * @return 返回结果
     */
    public static <T> RestRst build(int status, String msg) {
        return RestRst.build(status, msg, null);
    }

    public static <T> RestRst build(RestCode restCode) {
        return RestRst.build(restCode.getCode(), restCode.getMessage(), null);
    }

    public static <T> RestRst buildSuccess() {
        return RestRst.build(RestCode.SUCCESS.getCode(), RestCode.SUCCESS.getMessage(), null);
    }

    public static RestRst error(Integer status, String msg) {
        return RestRst.builder()
                .status(status)
                .msg(msg)
                .rspTime(System.currentTimeMillis())
                .data(null)
                .build();
    }
}
